import 'package:flutter/material.dart';
import 'package:navigator_demo/navigator1pointzero/bottom_bar_menu_page.dart';
import 'package:navigator_demo/navigator_warpper/page_navigator.dart';
import '../navigator_ver01.dart';

final subscribers = ['CommonPage'];

class TabA extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    context.owner;
    print('====== TabA  build ====== 1');
    return Scaffold(
      appBar: AppBar(
        title: Text('tab_a.dart `/`'),
      ),
      body: ListView(
        children: [
          ListTile(
            title: Text('navigator basic usage'),
            onTap: () {
              Navigator.of(context).pushNamed('/others');
            },
            trailing: Icon(Icons.arrow_right),
          ),
          ListTile(
            title: Text('goto tab2 /A/B/C'),
            onTap: () {
              try {
                context.findAncestorWidgetOfExactType<BottmBarMenuPage>()
                  ..tabIndexNotifier.value = 1
                  ..navigate = (_) {
                    PageNavigator.of(context)
                        .subpageNavigator
                        ?.pushNamed('C', arguments: [
                      'A',
                      'B',
                    ]);
                  };
              } on Object catch (e, s) {
                print('current is not under the `BottmBarMenuPage` tree $e,$s');
              }
            },
            trailing: Icon(Icons.arrow_right),
          ),
          ListTile(
            title: Text('push /A/B/C'),
            onTap: () {
              Navigator.of(context).pushNamed('C', arguments: [
                'A',
                'B',
              ]);
            },
            trailing: Icon(Icons.arrow_right),
          ),
          ListTile(
            title: Text('push /A/B/C/D/E'),
            onTap: () {
              Navigator.of(context).pushNamed('E', arguments: [
                'A',
                'B',
                'C',
                'D',
              ]);
            },
            trailing: Icon(Icons.arrow_right),
          ),
          ListTile(
              title: Text('change bottom tab'),
              onTap: () {
                context
                    .findAncestorWidgetOfExactType<HomePage>()
                    .bottomMenuPageNotifier
                    .value = (context
                            .findAncestorWidgetOfExactType<HomePage>()
                            .bottomMenuPageNotifier
                            .value +
                        1) %
                    2;
              }),
          ListTile(
            title: Text('simple push'),
            onTap: () {
              final homePage =
                  context.findAncestorWidgetOfExactType<HomePage>();

              final route = MaterialPageRoute(
                builder: (context) => Scaffold(
                  appBar: AppBar(
                    title: Text('subpage'),
                  ),
                  body: Container(
                    child: FlatButton(
                      child: Text('rootPush'),
                      onPressed: () {
                        final rootRouteDemo = MaterialPageRoute(
                            builder: (context) => Scaffold(
                                  appBar: AppBar(
                                    title: Text('title'),
                                  ),
                                  body: Container(
                                    child: Center(
                                      child: GestureDetector(
                                        child: Text(
                                            'RootNavigator root page change bottom bar'),
                                        onTap: () {
                                          print(
                                              "homePage xxx ${context.findAncestorWidgetOfExactType<HomePage>()}");
                                          homePage.bottomMenuPageNotifier
                                              .value = (homePage
                                                      .bottomMenuPageNotifier
                                                      .value +
                                                  1) %
                                              2;
                                          // final overlayEntries = ModalRoute.of(context).overlayEntries;
                                          // final rootNavigatorContext =  Navigator.of(context, rootNavigator: true).context;
                                          // print('context $context rootNavigatorContext: $rootNavigatorContext');
                                          // Overlay.of(context).insertAll(overlayEntries);
                                        },
                                      ),
                                    ),
                                  ),
                                ));
                        Navigator.of(context, rootNavigator: true)
                            .push(rootRouteDemo);
                      },
                    ),
                  ),
                ),
              );
              Navigator.of(context).push(route);
            },
          ),
        ],
      ),
    );
  }
}

/*
  @optionalTypeArgs
  static Future<T> pushReplacementNamed<T extends Object, TO extends Object>(
    BuildContext context,
    String routeName, {
    TO result,
    Object arguments,
  }) {
    return Navigator.of(context).pushReplacementNamed<T, TO>(routeName, arguments: arguments, result: result);
  }
  @optionalTypeArgs
  static Future<T> popAndPushNamed<T extends Object, TO extends Object>(
    BuildContext context,
    String routeName, {
    TO result,
    Object arguments,
   }) {
    return Navigator.of(context).popAndPushNamed<T, TO>(routeName, arguments: arguments, result: result);
  }
  @optionalTypeArgs
  static Future<T> pushNamedAndRemoveUntil<T extends Object>(
    BuildContext context,
    String newRouteName,
    RoutePredicate predicate, {
    Object arguments,
  }) {
    return Navigator.of(context).pushNamedAndRemoveUntil<T>(newRouteName, predicate, arguments: arguments);
  }
  @optionalTypeArgs
  static Future<T> push<T extends Object>(BuildContext context, Route<T> route) {
    return Navigator.of(context).push(route);
  }
  @optionalTypeArgs
  static Future<T> pushReplacement<T extends Object, TO extends Object>(BuildContext context, Route<T> newRoute, { TO result }) {
    return Navigator.of(context).pushReplacement<T, TO>(newRoute, result: result);
  }
  @optionalTypeArgs
  static Future<T> pushAndRemoveUntil<T extends Object>(BuildContext context, Route<T> newRoute, RoutePredicate predicate) {
    return Navigator.of(context).pushAndRemoveUntil<T>(newRoute, predicate);
  }
  @optionalTypeArgs
  static void replace<T extends Object>(BuildContext context, { @required Route<dynamic> oldRoute, @required Route<T> newRoute }) {
    return Navigator.of(context).replace<T>(oldRoute: oldRoute, newRoute: newRoute);
  }
  @optionalTypeArgs
  static void replaceRouteBelow<T extends Object>(BuildContext context, { @required Route<dynamic> anchorRoute, Route<T> newRoute }) {
    return Navigator.of(context).replaceRouteBelow<T>(anchorRoute: anchorRoute, newRoute: newRoute);
  }
  static bool canPop(BuildContext context) {
    final NavigatorState navigator = Navigator.of(context, nullOk: true);
    return navigator != null && navigator.canPop();
  }
  @optionalTypeArgs
  static Future<bool> maybePop<T extends Object>(BuildContext context, [ T result ]) {
    return Navigator.of(context).maybePop<T>(result);
  }
  @optionalTypeArgs
  static void pop<T extends Object>(BuildContext context, [ T result ]) {
    Navigator.of(context).pop<T>(result);
  }
  static void popUntil(BuildContext context, RoutePredicate predicate) {
    Navigator.of(context).popUntil(predicate);
  }
  static void removeRoute(BuildContext context, Route<dynamic> route) {
    return Navigator.of(context).removeRoute(route);
  }
 
  static void removeRouteBelow(BuildContext context, Route<dynamic> anchorRoute) {
    return Navigator.of(context).removeRouteBelow(anchorRoute);
  }
*/
